<?php
namespace Admin\Controller;
use Common\Controller\BaseController;
class CommonController extends BaseController {
	public $loginMarked;
	
	/**
	 +----------------------------------------------------------
	 * 初始化
	 * 如果 继承本类的类自身也需要初始化那么需要在使用本继承类的类里使用parent::_initialize();
	 +----------------------------------------------------------
	 */
	public function _initialize() {
		header("Content-Type:text/html; charset=utf-8");
		header('Content-Type:application/json; charset=utf-8');
		$this->loginMarked = md5(C('TOKEN.admin_marked'));
		$this->checkLogin();
		$this->assign("myinfo", $_SESSION['my_info']);
		$this->assign("site", C('SITE_INFO'));

		if ($_SESSION['my_info']['aid']==1) {
		return true;
		}
			$auth=new \Think\Auth();
		$rule_name=MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;

		$result=$auth->check($rule_name,$_SESSION['my_info']['aid']);
		if(!$result){
			$this->error('您没有权限访问',U('Admin/Index/welcome'));
		}

		//$this->getQRCode();
	}
	

	
	public function checkLogin() {
		if (isset($_COOKIE[$this->loginMarked])) {
			$cookie = explode("_", $_COOKIE[$this->loginMarked]);
			$timeout = C("TOKEN");
			if (time() > (end($cookie) + $timeout['admin_timeout'])) {
				setcookie("$this->loginMarked", NULL, -3600, "/");
				unset($_SESSION[$this->loginMarked], $_COOKIE[$this->loginMarked]);
				$this->error("登录超时，请重新登录", U("Public/index"));
			} else {
				if ($cookie[0] == $_SESSION[$this->loginMarked]) {
					setcookie("$this->loginMarked", $cookie[0] . "_" . time(), 0, "/");
				} else {
					setcookie("$this->loginMarked", NULL, -3600, "/");
					unset($_SESSION[$this->loginMarked], $_COOKIE[$this->loginMarked]);
					$this->error("帐号异常，请重新登录", U("Public/index"));
				}
			}
		} else {
			$this->redirect("Public/index");
		}
		return TRUE;
	}
	
	/**
	 +----------------------------------------------------------
	 * 验证token信息
	 +----------------------------------------------------------
	 */
	protected function checkToken() {
		if (IS_POST) {
			if (!M("Admin")->autoCheckToken($_POST)) {
				die(json_encode(array('status' => 0, 'info' => '令牌验证失败')));
			}
			unset($_POST[C("TOKEN_NAME")]);
		}
	}


	
	
}

?>